package com.sandking.ios;

import java.util.ArrayList;
import java.util.List;

import javapns.devices.Device;
import javapns.devices.implementations.basic.BasicDevice;
import javapns.notification.AppleNotificationServerBasicImpl;
import javapns.notification.PushNotificationManager;
import javapns.notification.PushNotificationPayload;
import javapns.notification.PushedNotification;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @UserName : SandKing
 * @DataTime : 2012-9-5 上午11:38:42
 * @Description ：苹果IOS推送消息
 */
public class Push_IOS {
	static Log log = LogFactory.getLog(Push_IOS.class);

	/**
	 * 执行发送
	 * 
	 * @param sandbox
	 *            是否是沙盒模式
	 * @param token
	 *            取得设备的标示
	 * @param path
	 *            证书的路径
	 * @param password
	 *            证书密码
	 * @param count
	 *            显示的数字
	 * @param message
	 *            发送的消息 内容
	 */
	public static final void sendMessage(final boolean sandbox,
			final String token, final String path, final String password,
			final int count, String message) {
		try {
			if (message == null || "".equals(message)) {
				return;
			}
			message = String.format("{'aps':{'alert':'%s'}}", message);
			PushNotificationPayload payLoad = PushNotificationPayload
					.fromJSON(message);
			payLoad.addBadge(count);
			if (count > 0) {
				payLoad.addSound("default"); // 铃音 默认
			}
			PushNotificationManager pushManager = new PushNotificationManager();

			pushManager
					.initializeConnection(new AppleNotificationServerBasicImpl(
							path, password, sandbox));

			List<PushedNotification> notifications = new ArrayList<PushedNotification>();

			Device device = new BasicDevice();

			device.setToken(token);

			PushedNotification notification = pushManager.sendNotification(
					device, payLoad, true);

			notifications.add(notification);

			List<PushedNotification> failedNotifications = PushedNotification
					.findFailedNotifications(notifications);

			List<PushedNotification> successfulNotifications = PushedNotification
					.findSuccessfulNotifications(notifications);

			int failed = failedNotifications.size();

			int successful = successfulNotifications.size();

			if (successful > 0 && failed == 0) {

				log.debug("-----All notifications pushed 成功 ("
						+ successfulNotifications.size() + "):");

			} else if (successful == 0 && failed > 0) {

				log.debug("-----All notifications 失败 ("
						+ failedNotifications.size() + "):");

			} else if (successful == 0 && failed == 0) {

				System.out
						.println("No notifications could be sent, probably because of a critical error");

			} else {
				log.debug("------Some notifications 失败 ("
						+ failedNotifications.size() + "):");

				log.debug("------Others 成功 (" + successfulNotifications.size()
						+ "):");

			}
			// pushManager.stopConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
